package com.qf.service.impl;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilerChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * projectName: sms-platform02
 *
 * @author: 元
 * time: 2021/10/18 0018 17:00
 * description:路由策略实现
 * 根据号码归属地不同,识别后发给不同运营商
 */
@Slf4j
@Service(value = "router")
public class RouterServiceImpl implements FilerChainService {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-路由策略],开始执行",standardSubmit);
        //1.封装队列名称,根据运营商ID进行分装
        String queueName= RabbitMQConstants.TOPIC_SMS_GATEWAY + standardSubmit.getGatewayID();

        //2.创建指定队列
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());
        //3.发送消息,根据运营商ID,将短信数据发送到对应的运营商队列中
        rabbitTemplate.convertAndSend(queueName,standardSubmit);
        log.info("[策略模块-路由策略],执行成功,短信数据路由到={}队列中,standardSubmit={}",queueName,standardSubmit);
        return true;
    }
}
